<?php

namespace app\admin\model\from;

use think\Model;
use think\Cache;
class Family extends Model
{
    // 表名
    protected $name = 'from_family';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $resultSetType = 'collection';
    // 追加属性
    protected $append = [
        'grade_text',
        'message_text',  
        'receive_text', 
    ];
      public function getReceiveList()
    {
        return ['1' => __('Receive 1'),'2' => __('Receive 2')];
    }     


    public function getReceiveTextAttr($value, $data)
    {    
        if(!isset($data['receive'])) return '';    
        $value = $value ? $value : $data['receive'];
        $list = $this->getReceiveList();
        return isset($list[$value]) ? $list[$value] : '';
    }      
    public function getMessageList()
    {
        return ['1' => __('Message_status 1'),'2' => __('Message_status 2')];
    }     


    public function getMessageTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['message_status'];
        $list = $this->getMessageList();
        return isset($list[$value]) ? $list[$value] : '';
    }
    
    public function getGradeList()
    {
        return ['1' => __('Grade 1'),'2' => __('Grade 2'),'3' => __('Grade 3'),'4' => __('Grade 4'),'5' => __('Grade 5'),'6' => __('Grade 6')];
    }     


    public function getGradeTextAttr($value, $data)
    {   
        if(!isset($data['grade'])) return '';     
        $value = $value ? $value : $data['grade'];
        $list = $this->getGradeList();
        return isset($list[$value]) ? $list[$value] : '';
    }
     /**
     * 获取省的名称
     * @return array
     */
    public function getProvinceOrderList()
    {
        if(!$list = \think\Cache::get('arealist'))
        {
            $area = model(GetTableModel('Area'))::field('id,name')->select();
            $area = collection($area)->toArray();
            $list = array_column($area,'name','id');
            Cache::set('areaList',$list,0);//设置0永久缓存不过期
        }
        return $list;
    }
      /**
     * 获取姓名
     * @param $userId
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public  function getuserName()
    {
        if(!$userlist = Cache::get('USERLIST'))
        {
            $userlist = collection(model(GetTableModel('Admin'))::field("userid,username,gender,isleader,department,delete_status")->select())->toArray();
            $userlist = array_column($userlist,null,'userid');
            Cache::set('USERLIST',$userlist,604800);//有效期一个星期
        }
        return $userlist;
    }



}
